//
//  PywBundGameAccountController.m
//  PywSdk
//
//  Created by smallwave on 16/8/29.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywBundGameAccountController.h"
#import "PywTextFieldView.h"
#import "UIButton+Pyw.h"
#import "PywMasonry.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywAccountBindToPassportApi.h"
#import "PywAlertView.h"
#import "PywUserViewController.h"

@interface PywBundGameAccountController ()<PywAlertViewDelegate>

@property (nonatomic, strong) PywTextFieldView  *gameAccountText;   // 游戏账号
@property (nonatomic, strong) PywTextFieldView  *accountPwdText;    // 账号密码
@property (nonatomic, strong) UIButton          *bundBtn;           // 绑定按钮

@property (nonatomic,copy) GameBundFinish       gameBundFinish;     // 绑定成功回调
@property (nonatomic, strong) PywAccountBindToPassportApi *accountBindToPassportApi;

@end

@implementation PywBundGameAccountController
KeyBoardHelper(90)

- (instancetype)initWithFinishBlock:(GameBundFinish)gameBundFinish
{
    self = [super init];
    if (self) {
        self.gameBundFinish = gameBundFinish;
    }
    return self;
}

#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}
#pragma mark - prepareUI
- (void)setUp{
    self.title = @"选择账号";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    [self.view addSubview:self.gameAccountText];
    [self.view addSubview:self.accountPwdText];
    [self.view addSubview:self.bundBtn];
    
    __weak typeof(self) wSelf = self;
    [self.gameAccountText mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
        make.height.equalTo(@40);
    }];
    [self.accountPwdText mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.gameAccountText.mas_bottom).offset(10);
        make.width.height.equalTo(wSelf.gameAccountText);
    }];
    [self.bundBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.accountPwdText.mas_bottom).offset(40);
        make.width.height.equalTo(wSelf.accountPwdText);
    }];
}
#pragma mark - Action
/**
 *  立即绑定
 */
- (void)bundAction{
    [self.view endEditing:YES];
    
    NSString *account = [self.gameAccountText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *account_pwd = [self.accountPwdText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (account.length == 0) {
        [PywMBProgressHUD hideHUD];
        [PywMBProgressHUD showError:@"请输入要绑定的游戏账号!"];
        
    } else if (account_pwd.length == 0) {
        [PywMBProgressHUD hideHUD];
        [PywMBProgressHUD showError:@"请输入密码!"];
        
    } else {
        
        WEAK_SELF;
        [weakSelf.accountBindToPassportApi requestAccountBindToPassportWithAccount:account account_pwd:account_pwd success:^(NSString *status, id anyObjct) {
            
            if (weakSelf.gameBundFinish) {
                weakSelf.gameBundFinish();
            }
        } fail:^(NSError *error) {
            PywAlertView *alertView = [[PywAlertView alloc] initWithTitle:@"温馨提示"
                                                                  message:@"友友手机绑定失败了哟！"
                                                               subMessage:@"该游戏账号已与其他手机绑定"
                                                             messageImage:@"img_emoji01"
                                                         messageAlignment:MessageAlignmentHorizontal
                                                                 delegate:weakSelf
                                                        cancelButtonTitle:@"返回用户中心"
                                                          sureButtonTitle:@"绑定其他账号"];
            [alertView show];
        }];
    }
    
}
#pragma mark - PywAlertViewDelegate
/**
 *  select Button
 *
 *  @param alertView   this AlertView
 *  @param buttonIndex select  ButtonIndex
 */
- (void)alertView:(PywAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) { // 返回用户中心
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
#pragma mark - Lazy
- (PywTextFieldView *)gameAccountText{
    if (!_gameAccountText) {
        _gameAccountText = [PywTextFieldView phoneOrAccountTextFieldWithLeftImg:@"icon_user_normal"
                                                                TextPlaceholder:@"请输入要绑定的游戏账号"];
        [_gameAccountText setAccessibilityId:@"binaccount"];
    }
    return _gameAccountText;
}
- (PywTextFieldView *)accountPwdText{
    if (!_accountPwdText) {
        _accountPwdText = [PywTextFieldView passwordTextFieldWithLeftImg:@"icon_password_normal"
                                                         TextPlaceholder:@"请输入密码"
                                                         ButtonNormalImg:@"icon_visible_normal"
                                                       ButtonSelectedImg:@"icon_visible_disabled"];
        [_accountPwdText setAccessibilityId:@"binaccount_psw"];
    }
    return _accountPwdText;
}
- (UIButton *)bundBtn{
    if (!_bundBtn) {
        _bundBtn = [UIButton buttonWithBackgroundColor:UIColorFromRGB(0xFFBC00)
                                          cornerRadius:20
                                            titleColor:[UIColor whiteColor]
                                    titleLabelFontSize:18
                                                 title:@"立即绑定"];
        
        [_bundBtn addTarget:self action:@selector(bundAction) forControlEvents:UIControlEventTouchUpInside];
        [_bundBtn setAccessibilityIdentifier:@"binaccount_sure"];
    }
    return _bundBtn;
}
/**
 *  账号绑定到通行证
 *
 *  @return <#return value description#>
 */
- (PywAccountBindToPassportApi *)accountBindToPassportApi{
    if (!_accountBindToPassportApi) {
        _accountBindToPassportApi = [PywAccountBindToPassportApi manager];
    }
    return _accountBindToPassportApi;
}

@end
